package com.jsoft.android.exp.ffxiii;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class Exp extends Activity {

	static EditText lvCurr;

	public static final int CHARACTER_PICKER = 1;
	public static final int WEAPONS_PICKER = 2;

	public static final String KEY_TITLE = "title";
	public static final String KEY_BODY = "body";
	public static final String KEY_POSITION = "position";
	
	public static final int KEY_LIGHTNING = 0;
	public static final int KEY_SAZH = 1;
	public static final int KEY_SNOW = 2;
	public static final int KEY_HOPE = 3;
	public static final int KEY_VANELLE = 4;
	public static final int KEY_FANG = 5;

	private TextView mCurrentCharacter;

	private EditText myLvCurrent;
	private EditText myLvTarget;
	public static EditText myCurrentEXP;

	// private???

	RadioButton mCurrentTier1Weapon;
	RadioButton mCurrentTier2Weapon;
	RadioButton mCurrentTier3Weapon;

	// move to Inventory?
	static int mCurrentLv = 1;
	static int mTargetLv = 26;;

	static int mCurrentEXP = 0;

	TextView mTargetEXP;

	TextView mCurrentStr;
	TextView mTargetStr;
	TextView mCurrentMagic;
	TextView mTargetMagic;

	//
	static String character;

	
	public static int nameSearch(String[] names, String name) {
		for (int n = 0; n < names.length; n++) {
			if (names[n].equals(name))
				return n;
		}

		return -1;
	}
	
	Resources res;
	String[] characters_in;
	String[] characters_en;
	
	/** Called when the activity is first created. */
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		res = getResources();
		characters_in = res.getStringArray(R.array.characters_array);
		characters_en = res.getStringArray(R.array.characters_en_array);
		
	    character = characters_in[KEY_LIGHTNING];

		// set up to default to Lightning when starting
		Inventory.setCurrentWeapons(Inventory.getAwg(characters_in[KEY_LIGHTNING]));

		// the first group - also sets the current weapon to tier1
		Inventory.setCurrentWeaponTier(Inventory.getAwg(characters_in[KEY_LIGHTNING])[0]);
		//Inventory.setCurrentWeaponTier(Inventory.getCurrentWeapons()[0]);

		// set fields
		mCurrentCharacter = (TextView) findViewById(R.id.CurrentCharacter);
		mCurrentTier1Weapon = (RadioButton) findViewById(R.id.tier1);
		mCurrentTier2Weapon = (RadioButton) findViewById(R.id.tier2);
		mCurrentTier3Weapon = (RadioButton) findViewById(R.id.tier3);

		mTargetEXP = (TextView) findViewById(R.id.TargetEXP);

		mCurrentStr = (TextView) findViewById(R.id.CurrentStr);
		mTargetStr = (TextView) findViewById(R.id.TargetStr);
		mCurrentMagic = (TextView) findViewById(R.id.CurrentMagic);
		mTargetMagic = (TextView) findViewById(R.id.TargetMagic);

		//
		final Button charactersButton = (Button) findViewById(R.id.CharacterPicker);
		charactersButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Perform action on clicks
				// Toast.makeText(Exp.this, "Beep Bop",
				// Toast.LENGTH_SHORT).show();

				Intent myIntent = new Intent();
				myIntent.setClassName("com.jsoft.android.exp.ffxiii", "com.jsoft.android.exp.ffxiii.CharactersPicker"); 
				// startActivity(myIntent);
				startActivityForResult(myIntent, CHARACTER_PICKER);
			}
		});

		//
		final Button weaponsButton = (Button) findViewById(R.id.WeaponsPicker);
		weaponsButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Perform action on clicks
				// Toast.makeText(Exp.this, "Beep Bop",
				// Toast.LENGTH_SHORT).show();

				Intent myIntent = new Intent();
				myIntent.setClassName("com.jsoft.android.exp.ffxiii", "com.jsoft.android.exp.ffxiii.WeaponsPicker"); //$NON-NLS-1$

				// startActivity(myIntent);
				startActivityForResult(myIntent, WEAPONS_PICKER);
			}
		});

		//
		final RadioButton radio_tier1 = (RadioButton) findViewById(R.id.tier1);
		final RadioButton radio_tier2 = (RadioButton) findViewById(R.id.tier2);
		final RadioButton radio_tier3 = (RadioButton) findViewById(R.id.tier3);
		radio_tier1.setOnClickListener(radio_listener1);
		radio_tier2.setOnClickListener(radio_listener2);
		radio_tier3.setOnClickListener(radio_listener3);

		//
		final EditText lvCurrent = (EditText) findViewById(R.id.CurrentLv);
		myLvCurrent = lvCurrent;
		lvCurrent.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					// update
					mCurrentLv = Integer.parseInt(lvCurrent.getText()
							.toString().trim());
					// Inventory.getCurrentWeapon().setBaseLevel(Integer.parseInt(lvCurrent.getText().toString().trim()));
					// Toast.makeText(Exp.this,
					// "got the Lv Current "+lvCurrent.getText(),
					// Toast.LENGTH_SHORT).show();
					// Toast.makeText(Exp.this, "mCurrentLv is "+mCurrentLv,
					// Toast.LENGTH_SHORT).show();
					calc();
					return true;
				}
				return false;
			}
		});

		final EditText lvTarget = (EditText) findViewById(R.id.TargetLv);
		myLvTarget = lvTarget;
		lvTarget.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					mTargetLv = Integer.parseInt(lvTarget.getText().toString()
							.trim());
					Toast.makeText(Exp.this, "got the Lv Target "  + lvTarget.getText(),
							Toast.LENGTH_SHORT).show();
					calc();
					return true;
				}
				return false;
			}
		});
		
		//
		final EditText currentEXP = (EditText) findViewById(R.id.CurrentEXP);
		myCurrentEXP = currentEXP;

		currentEXP.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					mCurrentEXP = Integer.parseInt(currentEXP.getText()
							.toString().trim());
					Toast.makeText(Exp.this,
							"got the current EXP " + currentEXP.getText(),
							Toast.LENGTH_SHORT).show();
					calc();
					return true;
				}
				return false;
			}
		});

		// all of them?
		lvTarget.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				Toast.makeText(Exp.this, "beforeTextChanged",
						Toast.LENGTH_SHORT).show();

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				Toast.makeText(Exp.this, "onTextChanged", Toast.LENGTH_SHORT)
						.show();

			}

			@Override
			public void afterTextChanged(Editable s) {
				mCurrentLv = Integer.parseInt(lvCurrent.getText().toString()
						.trim());
				myLvCurrent.setText(Integer.toString(mCurrentLv));

				// TODO myLvTarget causes a bug?
				mTargetLv = Integer.parseInt(lvTarget.getText().toString().trim());
				//myLvTarget.setText(Integer.toString(mTargetLv));
				//lvTarget.setText(Integer.toString(mTargetLv));

				mCurrentEXP = Integer.parseInt(currentEXP.getText()
						.toString().trim());
				myCurrentEXP.setText(Integer.toString(mCurrentEXP));

				Toast.makeText(Exp.this, "afterTextChanged", Toast.LENGTH_SHORT)
						.show();
			}

		});

		// only place this event entered
		lvTarget.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// when I click on a new EditText
				Toast.makeText(Exp.this, "onFocusChange", Toast.LENGTH_SHORT)
						.show();
			}
		});

		


	}

	private OnClickListener radio_listener1 = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// Perform action on clicks
			Inventory.setCurrentWeapon(Inventory.getCurrentWeaponTier().getTier1());


			// current and target Lv and current EXP
			mCurrentLv = Inventory.getCurrentWeapon().getBaseLevel();
			myLvCurrent.setText(Integer.toString(mCurrentLv));

			mTargetLv = Inventory.getCurrentWeapon().getTargetLevel();
			myLvTarget.setText(Integer.toString(mTargetLv));

			mCurrentEXP = Inventory.getCurrentWeapon().getStartingEXP();
			//myCurrentEXP.setText(Integer.toString(mCurrentEXP));

			calc();

			// RadioButton rb = (RadioButton) v;
			// Toast.makeText(Exp.this, rb.getText(),
			// Toast.LENGTH_SHORT).show();
		}
	};

	private OnClickListener radio_listener2 = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// Perform action on clicks
			Inventory.setCurrentWeapon(Inventory.getCurrentWeaponTier().getTier2());

			// current and target Lv and current EXP
			mCurrentLv = Inventory.getCurrentWeapon().getBaseLevel();
			myLvCurrent.setText(Integer.toString(mCurrentLv));

			mTargetLv = Inventory.getCurrentWeapon().getTargetLevel();
			myLvTarget.setText(Integer.toString(mTargetLv));

			mCurrentEXP = Inventory.getCurrentWeapon().getStartingEXP();
			myCurrentEXP.setText(Integer.toString(mCurrentEXP));

			calc();

			// RadioButton rb = (RadioButton) v;
			// Toast.makeText(Exp.this, rb.getText(),
			// Toast.LENGTH_SHORT).show();
		}
	};
	private OnClickListener radio_listener3 = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// Perform action on clicks
			Inventory.setCurrentWeapon(Inventory.getCurrentWeaponTier().getTier3());

			// current and target Lv and current EXP
			mCurrentLv = Inventory.getCurrentWeapon().getBaseLevel();
			myLvCurrent.setText(Integer.toString(mCurrentLv));

			mTargetLv = Inventory.getCurrentWeapon().getTargetLevel();
			myLvTarget.setText(Integer.toString(mTargetLv));

			mCurrentEXP = Inventory.getCurrentWeapon().getStartingEXP();
			myCurrentEXP.setText(Integer.toString(mCurrentEXP));

			calc();

			// RadioButton rb = (RadioButton) v;
			// Toast.makeText(Exp.this, rb.getText(),
			// Toast.LENGTH_SHORT).show();
		}
	};

	// Listen for results.
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Bundle extras = data.getExtras();

		// See which child activity is calling us back.
		switch (requestCode) {
		case CHARACTER_PICKER:
			// This is the standard resultCode that is sent back if the
			// activity crashed or didn't doesn't supply an explicit result.
			if (resultCode == RESULT_OK) {
				String who = extras.getString(KEY_TITLE);
				character = characters_en[nameSearch(characters_in, who)];
				//
				mCurrentCharacter.setText(who);
				// all the weapons
				// TODO get back to "Lightning"
				
				//if (character.equals("ライトニング")) who="Lightning";
				Inventory.setCurrentWeapons(Inventory.getAwg(who));

				// the first group - also sets the current weapon to tier1
				Inventory
						.setCurrentWeaponTier(Inventory.getCurrentWeapons()[0]);

				
				// the weapon names in radio buttons
				mCurrentTier1Weapon.setText(Inventory.getCurrentWeaponTier()
						.getTier1().getName());
				mCurrentTier2Weapon.setText(Inventory.getCurrentWeaponTier()
						.getTier2().getName());
				mCurrentTier3Weapon.setText(Inventory.getCurrentWeaponTier()
						.getTier3().getName());

				// current and target Lv and current EXP
				mCurrentLv = Inventory.getCurrentWeapon().getBaseLevel();
				myLvCurrent.setText(Integer.toString(mCurrentLv));

				mTargetLv = Inventory.getCurrentWeapon().getTargetLevel();
				myLvTarget.setText(Integer.toString(mTargetLv));
				//myLvCurrent.setText(Integer.toString(mTargetLv));

				
				mCurrentEXP = Inventory.getCurrentWeapon().getStartingEXP();
				myCurrentEXP.setText(Integer.toString(mCurrentEXP));

				// Toast.makeText(Exp.this, who, Toast.LENGTH_SHORT).show();
				calc();
			} else {
				Toast.makeText(Exp.this, "huh", Toast.LENGTH_SHORT).show(); 
				// myFightFunction(data);
			}
			break;

		case WEAPONS_PICKER: {
			if (resultCode == RESULT_OK) {

				// don't put t1 into bundle
				// String t1 = extras.getString(KEY_TITLE);
				int position = extras.getInt(KEY_POSITION);

				//
				Inventory
						.setCurrentWeaponTier(Inventory.getCurrentWeapons()[position]);

				// set the radio buttons names
				mCurrentTier1Weapon.setText(Inventory.getCurrentWeaponTier()
						.getTier1().getName());
				mCurrentTier2Weapon.setText(Inventory.getCurrentWeaponTier()
						.getTier2().getName());
				mCurrentTier3Weapon.setText(Inventory.getCurrentWeaponTier()
						.getTier3().getName());

				// current and target Lv and current EXP
				mCurrentLv = Inventory.getCurrentWeapon().getBaseLevel();
				myLvCurrent.setText(Integer.toString(mCurrentLv));

				mTargetLv = Inventory.getCurrentWeapon().getTargetLevel();
				myLvTarget.setText(Integer.toString(mTargetLv));

				mCurrentEXP = Inventory.getCurrentWeapon().getStartingEXP();
				myCurrentEXP.setText(Integer.toString(mCurrentEXP));

				calc();

				// Toast.makeText(Exp.this, who, Toast.LENGTH_SHORT).show();
				// myMessageboxFunction("Fight cancelled");
			} else {
				Toast.makeText(Exp.this, "huh", Toast.LENGTH_SHORT).show();
				// myFightFunction(data);
			}
			break;

		}
		default:
			Toast.makeText(Exp.this, "de fault", Toast.LENGTH_SHORT).show();

			break;
		}
	}

	protected void calc() {
		// int levels = Inventory.getCurrentWeapon().getBaseLevel() - 1;

		int baseEXP = Inventory.getCurrentWeapon().getBaseEXP();
		int neededEXP = Inventory.getCurrentWeapon().getIncrEXP();
		// Inventory.currentWeapon.setBaseStr(16);
		int baseStr = Inventory.getCurrentWeapon().getBaseStr();
		int strIncr = Inventory.getCurrentWeapon().getStrIncr();
		int baseMagic = Inventory.getCurrentWeapon().getBaseMagic();
		int magicIncr = Inventory.getCurrentWeapon().getMagicIncr();

		mCurrentStr.setText(Integer.toString(baseStr
				+ (strIncr * (mCurrentLv - 1))));
		// mCurrentStr.setText(Integer.toString(baseStr+(strIncr * levels)));
		mCurrentMagic.setText(Integer.toString(baseMagic
				+ (magicIncr * (mCurrentLv - 1))));

		mTargetStr.setText(Integer.toString(baseStr
				+ (strIncr * (mTargetLv - 1))));
		mTargetMagic.setText(Integer.toString(baseMagic
				+ (magicIncr * (mTargetLv - 1))));

		int exp = Inventory.getCurrentWeapon().getStartingEXP();

		for (int n = 1; n < mCurrentLv; n++) { // get us to the current level
			exp = exp + baseEXP + (neededEXP * (n - 1));
		}

		// Toast.makeText(Exp.this, "exp "+exp, Toast.LENGTH_SHORT).show();

		for (int n = mCurrentLv; n < mTargetLv; n++) { // is the cast correct
			exp = exp + baseEXP + (neededEXP * (n - 1));
		}
		mTargetEXP.setText(Integer.toString(exp - mCurrentEXP));
		// Toast.makeText(Exp.this, "exp "+exp, Toast.LENGTH_SHORT).show();
	}
}